/**Assessment Setting
 * Allow change answer:Yes
 * Check Point
 * All type questions,answer can be change(Previous/Next)
 */
package com.tcc.assessment.studentrole.allowchangeanswer;

import org.apache.xpath.operations.String;
import org.testng.Assert;
import org.testng.annotations.Test;

import com.tcc.assessment.logic.Playback;
import com.tcc.qa.test.util.BaseTest;
import com.tcc.qa.test.util.SelLogger;

public class Assessment5 extends BaseTest {
  int errorCount = 0;

  //Initialization Log
  private static final SelLogger logger = SelLogger.getLogger(Assessment5.class);

  @Test(description = "Goto My Assessment Page and Start Session")
  public void A_setUp() throws InterruptedException {
    Playback.initPlaybacker(getWebDriver(), "Assessment5");
  }

  @Test(description = "Page1 Answer Question")
  public void B_page1() throws InterruptedException {
    getWebDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    Playback.switchWindow(getWebDriver());
    Playback.getShortAnswerObj(getWebDriver(), 2).sendKeys("2,BeforeModify");
    Playback.getChooseOneAsnwerObj(getWebDriver(), 3, 1).click();
    Playback.goNextPlaybackPage(getWebDriver());
  }

  @Test(description = "Page2 Answer Question")
  public void C_page2() throws InterruptedException {
    getWebDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    Playback.switchWindow(getWebDriver());
    Playback.getEssayObj(getWebDriver(), 1).sendKeys("13579, Before Modify");
  }

  @Test(description = "Abort Session and Close PlayBack Window")
  public void D_abortAndClose() throws InterruptedException {
    Playback.abortPlaybacker(getWebDriver());
  }

  @Test(description = "Resume Answer Assessment")
  public void E_resume() throws InterruptedException {
    Playback.resumePlaybacker(getWebDriver(), "Assessment5");
  }

  @Test(description = "Page1 Check and Change Answer")
  public void F_checkPage1() throws InterruptedException {
    getWebDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    Playback.switchWindow(getWebDriver());
    String pageCheck = Playback.getPageNavInfo(getWebDriver());
    String titleCheck = Playback.getTitleInfo(getWebDriver());
    Assert.assertTrue(pageCheck.contains("Page 1 of 3"), "Page1 >> Pagination info is incorrect! It show as" + pageCheck);
    Assert.assertTrue(titleCheck.contains("Assessment5"), "Page1 >> Page Title is incorrect!");

    if (Playback.isExistShortAnswerObj(getWebDriver(), 2)) {
      logger.pass("Page1 >> Check 'Short Answer' allow change answer Passed!");
    } else {
      logger.error("Page1 >> Check 'Short Answer' allow change answer Failed!");
      errorCount++;
    }
    if (Playback.getShortAnswerValue(getWebDriver(), 2).equals("2,BeforeModify")) {
      logger.pass("Page1 >> Check 'Short Answer' content Passed!");
    } else {
      logger.error("Page1 >> Check 'Short Answer' content Failed!");
      errorCount++;
    }
    Playback.getShortAnswerObj(getWebDriver(), 2).clear();
    Playback.getShortAnswerObj(getWebDriver(), 2).sendKeys("2,AfterModify");

    if (Playback.isExistChooseOneAsnwerObj(getWebDriver(), 3)) {
      logger.pass("Page1 >> Check 'Choose One' allow change answer Passed!");
    } else {
      logger.error("Page1 >> Check 'Choose One' allow change answer Failed!");
      errorCount++;
    }
    if (Playback.getChooseOneAsnwerObj(getWebDriver(), 3, 1).isSelected()) {
      logger.pass("Page1 >> Check 'Choose One' content Passed!");
    } else {
      logger.error("Page1 >> Check 'Choose One' content Failed!");
      errorCount++;
    }
    Playback.getChooseOneAsnwerObj(getWebDriver(), 3, 4).click();
    Playback.goNextPlaybackPage(getWebDriver());
  }

  @Test(description = "Page2 Check and Resume Answer")
  public void G_checkPage2() throws InterruptedException {
    getWebDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    Playback.switchWindow(getWebDriver());
    String pageCheck = Playback.getPageNavInfo(getWebDriver());
    Assert.assertEquals(pageCheck, "Page 2 of 3", "Page1 >> Pagination info is incorrect! It show as" + pageCheck);

    if (Playback.isExistEssayObj(getWebDriver(), 1)) {
      logger.pass("Page2 >> Check 'Essay' allow change answer Passed!");
    } else {
      logger.error("Page2 >> Check 'Essay' allow change answer Failed!");
      errorCount++;
    }

    if (Playback.getEssayValue(getWebDriver(), 1).equals("13579, Before Modify")) {
      logger.pass("Page2 >> Check 'Essay' content Passed!");
    } else {
      logger.error("Page2 >> Check 'Essay' content Failed!");
      errorCount++;
    }
    Playback.getEssayObj(getWebDriver(), 1).clear();
    Playback.getEssayObj(getWebDriver(), 1).sendKeys("13579,After Modify");

    if (Playback.isExistFillInBlankObj(getWebDriver(), 2)) {
      logger.pass("Page2 >> Check 'Fill In Blank' allow change answer Passed!");
    } else {
      logger.error("Page2 >> Check 'Fill In Blank' allow change answer Failed!");
      errorCount++;
    }

    Playback.getFillInBlankObj(getWebDriver(), 2).sendKeys("12, After Modify");
    Playback.goNextPlaybackPage(getWebDriver());
  }

  @Test(description = "Page3 Answer Question")
  public void H_page3() throws InterruptedException {
    getWebDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    Playback.switchWindow(getWebDriver());
    if (Playback.isExistShortAnswerObj(getWebDriver(), 1)) {
      logger.pass("Page3 >> Check 'Short Answer' allow change answer Passed!");
    } else {
      logger.error("Page3 >> Check 'Short Answer' allow change answer Failed!");
      errorCount++;
    }
    Playback.getShortAnswerObj(getWebDriver(), 1).sendKeys("6,BeforeModify");

    if (Playback.isExistMatchingOneObj(getWebDriver(), 2)) {
      logger.pass("Page3 >> Check 'Matching One' allow change answer Passed!");
    } else {
      logger.error("Page3 >> Check 'Matching One' allow change answer Failed!");
      errorCount++;
    }

    for (int i = 0; i < 5; i++) {
      Playback.getMatchingOneObj(getWebDriver(), 2, i + 1).sendKeys(String.valueOf(i + 1));
    }
  }

  @Test(description = "Finish Session and Close PlayBack Window")
  public void I_finishAndClose() throws InterruptedException {
    Playback.finishAndClosePlaybacker(getWebDriver());
  }

  @Test(description = "errorInfo")
  public void J_errorInfo() throws InterruptedException {
    System.out.println("The error count is " + errorCount);
    Assert.assertTrue(errorCount == 0);
  }
}
